package com.zb.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author WEN
 * @Date 2023/7/11 0011
 **/
@Data
@ApiModel("分页结果封装")
public class PageUtil {
    @ApiModelProperty(value = "当面页码", required = true)
    private Long page;

    @ApiModelProperty(value = "每页显示数量", required = true)
    private Long limit;

    public static void setPageLimit(PageUtil pageUtil) {
        pageUtil.setPage(null == pageUtil.getPage() || pageUtil.page <= 0? 1 : pageUtil.page);
        pageUtil.setLimit(null == pageUtil.getLimit() || pageUtil.getLimit() <= 0? 50 : pageUtil.getLimit());
    }

    public static void setPageLimitMobile(PageUtil pageUtil) {
        pageUtil.setPage(null == pageUtil.getPage() || pageUtil.page <= 0? 1 : pageUtil.page);
        pageUtil.setLimit(50L);
    }

    public static void setSelectPageLimit(PageUtil pageUtil) {
        pageUtil.setPage(null == pageUtil.getPage() || pageUtil.page <= 0? 1 : pageUtil.page);
        pageUtil.setLimit(null == pageUtil.getLimit() || pageUtil.getLimit() <= 0? 100 : pageUtil.getLimit());
    }

    /**
     * 分页结果封装
     * @param currentPage 当着页码
     * @param limit 每页显示数量
     * @param total 总记录数
     * @param data 分页的数据
     * @return
     */
    public static Map<String, Object> pageList(long currentPage, long limit, long total, Object data) {
        Map<String, Object> result = new HashMap<>();
        result.put("currentPage", currentPage);
        result.put("limit", limit);
        result.put("total", total);
        result.put("rows", data);
        return  result;
    }

    public static Map<String, Object> rows(Object data) {
        Map<String, Object> result = new HashMap<>();
        result.put("rows", data);
        return  result;
    }

    public static Map<String, Object> row(Object data) {
        Map<String, Object> result = new HashMap<>();
        result.put("row", data);
        return  result;
    }
}
